package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbCwMapper;

import com.it.tydic.estate.module.model.inner.Guanlichu;
import com.it.tydic.estate.module.model.inner.TbCw;
import com.it.tydic.estate.module.model.inner.TbCwsf;
import com.it.tydic.estate.module.service.CwglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:   车位区域管理ServiceImpl
 * @author: zhangshuo
 * @createDate: 2020/03/02
 * @version: 1.0
 */
@Service
@Transactional
public class CwglServiceImpl implements CwglService {
    @Resource
    private TbCwMapper tbCwMapper;
    /*条件查询*/
    @Override
    public Result selectByPage(Map<String,Object> tbCw) {
        Map<String,Object> map = new HashMap<>();

        if(tbCw.containsKey("pageNum") && tbCw.containsKey("pageSize")){//判断是否有分页条件有进行分页
            PageHelper.startPage((Integer) tbCw.get("pageNum"), (Integer) tbCw.get("pageSize"));
        }
        Page<TbCw> cheweiquyu = tbCwMapper.selectByPage(tbCw);
        List<TbCw> result = cheweiquyu.getResult();
        map.put("result",result);
        map.put("totle",cheweiquyu.getTotal());
        return   Result.success(map);
    }
    /*批量删除*/
    @Override
    public Result deleteByTbCw(List<String> list) {
        if(list==null || list.size()<=0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        int result = tbCwMapper.deleteByTbCw(list);
        if(result <= 0){
            return   Result.error(CodeMsg.DELETE_ERROR);
        }
        return Result.success();
    }
    /*新增或修改*/
    @Override
    public Result insertTbCwOrUpdata(TbCw tbCw)  {
        long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
        if(tbCw.getcwId() == null || "".equals(tbCw.getcwId())){//判断是否有主键
            tbCw.setcwId(String.valueOf(id));
            tbCw.setCwZfbj("0");
            tbCw.setcwCjsj(new Date());
            tbCw.setcwXgsj(new Date());
            int result = tbCwMapper.insertSelective(tbCw);
            if(result < 0)
                return   Result.error(CodeMsg.INSERT_ERROR);
        }else{
            tbCw.setcwXgsj(new Date());
            int result = tbCwMapper.updateByPrimaryKeySelective(tbCw);
        }
        return   Result.success(tbCw);
    }
}
